package org.ggn.scoreanalyze.view;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;

/**
 * Created by guin_guo on 2015/12/12.
 */
public class MyTableModel {
    private static JTable table;
    private static DefaultTableModel tableModel;

    public MyTableModel() {
    }

    public static JTable defaultTable(){
        String[] columnNames = {"学号", "姓名","分数"};
        tableModel = new DefaultTableModel(){
            //让单元格不能编辑
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        tableModel.setDataVector(null, columnNames);
        table = new JTable(tableModel);
        table.setShowGrid(false);
        table.setShowHorizontalLines(false);
        table.setShowVerticalLines(false);
        //不允许拖动
        table.setDragEnabled(false);
        table.getTableHeader().setReorderingAllowed(false);
        table.getTableHeader().setResizingAllowed(false);
        //表头排序
        table.setAutoCreateRowSorter(true);
        return table;
    }
    public static DefaultTableModel getTableModel(Vector<Vector<String>> row){
        tableModel = new DefaultTableModel(){
            //让单元格不能编辑
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        String[] tableTiles = {"学号", "姓名","分数"};
        Vector tableTitle = new Vector(3);
        tableTitle.add(tableTiles[0]);
        tableTitle.add(tableTiles[1]);
        tableTitle.add(tableTiles[2]);
        tableModel.setDataVector(row, tableTitle);
        if (row != null) {
            tableModel.setRowCount(row.size());
        }
        return tableModel;
    }
}
